﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CrossGenLogic
{
    public class Crossword
    {
        private int width, height;
        private List<GridElement> grid;
        private List<VocElement> vocabulary;
        private CharWithCount[,] field;

        public Crossword(List<GridElement> grid, List<VocElement> vocabulary, int width, int height)
        {
            this.height = height;
            this.width = width;
            this.grid = grid;
            this.vocabulary = vocabulary;
            field = new CharWithCount[height, width];
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                    field[i, j] = new CharWithCount();
        }

        public int Height
        {
            get
            {
                return height;
            }
        }

        public int Width
        {
            get
            {
                return width;
            }
        }

        public void DecreaseCount(int i, int j)
        {
            field[j, i].DecreaseCount();
        }

        public char GetCharWithCount(int i, int j)
        {
            return field[j, i].Char;
        }

        public void SetCharWithCount(int i, int j, char letter)
        {
            if (i < width && j < height)
                field[j, i].Char = letter;          
        }

        public GridElement GetGEByNumber(int i)
        {
            return grid[i];
        }

        public VocElement GetWordByNumber(int i)
        {
            return vocabulary[i];
        }

        public int WordsInCross()
        {
            return grid.Count;
        }

        public int WordsInVoc()
        {
            return vocabulary.Count;
        }

        private int CompareByLength(VocElement x, VocElement y)
        { 
            if (x.Length > y.Length) return 1;
            if (x.Length == y.Length) return 0;
            else return -1;
        }

        public void SortVocabulary()
        {
            vocabulary.Sort(CompareByLength);
        }

        public void SetBusy(int i, bool b)
        {
            vocabulary[i].Busy = b;
        }
    }
}
